package com.zhangkunhao.notes.service;

import com.zhangkunhao.notes.dao.AnswerCommentDao;
import com.zhangkunhao.notes.entity.Do.AnswerComment;
import com.zhangkunhao.notes.entity.Do.PermissionBlacklist;
import com.zhangkunhao.notes.entity.bo.LegalWordsDetector;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.impl.AnswerCommentService;
import com.zhangkunhao.notes.utils.IfValidInputUtil;
import com.zhangkunhao.notes.utils.IllegalWordsDetectorUtils;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 评论接口的实现类
 *
 * @author 20642
 */
public class AnswerCommentServiceImpl implements AnswerCommentService {

    @Override
    public List<AnswerComment> selectAll() {
        return AnswerCommentDao.getInstance().findAll();
    }

    @Override
    public void insert(AnswerComment answerComment) throws Exception {
        AnswerCommentDao.getInstance().insert(answerComment);
    }

    @Override
    public int delete(int id) throws SQLException {
        return AnswerCommentDao.getInstance().delete(id);
    }

    @Override
    public int update(AnswerComment answerComment, String... params) throws Exception {
        return AnswerCommentDao.getInstance().update(answerComment);
    }

    public Message<AnswerComment> publishComment(AnswerComment answerComment) throws Exception {
        //初始化
        Message<AnswerComment> commentMessage = new Message<>(null, "你已经被回答该问题的用户拉黑，评论失败", -1);
        //调用dao校验是否是已经拉黑的用户
        boolean anyMatch = new BlackListServiceImpl().selectAll().stream()
                .anyMatch(blackList -> blackList.getFromUserId().equals(answerComment.getAnswerUserId())
                        && blackList.getForUserId().equals(answerComment.getAnswerCommentUserId()));
        if (anyMatch) {
            System.out.println("你已经被回答该问题的用户拉黑，发表评论失败");
            return commentMessage;
        }

        //校验
        if (answerComment == null) {
            commentMessage.setStatus(0);
            commentMessage.setResult("当前评论为空");
            return commentMessage;
        } else {
            if (!(IfValidInputUtil.ifValidValue(answerComment.getContent())) || !(IfValidInputUtil.emptyCheckPass(answerComment.getContent()))) {
                commentMessage.setStatus(0);
                commentMessage.setResult("当前评论内容为空");
                return commentMessage;
            }
            if (!(IfValidInputUtil.ifValidValue(answerComment.getCommentUserName()))) {
                commentMessage.setStatus(0);
                commentMessage.setResult("当前评论的用户名为空");
                return commentMessage;
            }
        }

        //调用工具类进行敏感词汇，如诋毁党、⾊情等信息检测
        LegalWordsDetector contentCheck = IllegalWordsDetectorUtils.isLegalWords(answerComment.getContent());
        if (!contentCheck.isIsLegal()) {
            commentMessage.setStatus(0);
            commentMessage.setResult("问题内容" + contentCheck.getMsg());
            return commentMessage;
        }


        //调用dao
        this.insert(answerComment);

        List<AnswerComment> questionList = this.selectAll();

        for (AnswerComment answerComment2 : questionList) {
            if (answerComment2.getAnswerCommentUserId().equals(answerComment.getAnswerCommentUserId())
                    && answerComment2.getContent().equals(answerComment.getContent())
                    && answerComment2.getAnswerId().equals(answerComment.getAnswerId())
                    && answerComment2.getAnswerUserId().equals(answerComment.getAnswerUserId())) {
                System.out.println("publishCommentSuccess");
                commentMessage.setResult("评论发布成功,已刷新当前页面");
                commentMessage.setStatus(1);
                commentMessage.setT(answerComment2);
                break;
            }
        }
        return commentMessage;
    }

    public Message<AnswerComment> reportComment(int reportCommentId, String reportCommentReason) throws Exception {
        //查找修改数据
        List<AnswerComment> reportComment = this.selectAll().stream()
                .filter(comment -> comment.getId().equals(reportCommentId)).collect(Collectors.toList());
        reportComment.get(0).setReportStatus(0);
        reportComment.get(0).setReportIllegalReason(reportCommentReason);

        System.out.println("被举报的评论对象 " + reportComment.get(0));

        //初始化
        Message<AnswerComment> reportQuestionMessage = new Message<>();
        reportQuestionMessage.setT(null);
        reportQuestionMessage.setStatus(0);

        //校验
        if (reportComment.get(0) == null) {
            reportQuestionMessage.setResult("当前被举报的评论为空");
            return reportQuestionMessage;
        } else if (!(IfValidInputUtil.ifValidValue(reportComment.get(0).getReportIllegalReason()))) {
            reportQuestionMessage.setResult("评论的举报理由为空，请重新填写");
            return reportQuestionMessage;
        }

        //调用dao
        int update = this.update(reportComment.get(0));

        if (update > 0) {
            System.out.println("reportCommentSuccess");
            reportQuestionMessage.setResult("评论举报成功,已刷新当前页面");
            reportQuestionMessage.setStatus(1);
            reportQuestionMessage.setT(reportComment.get(0));
        } else {
            reportQuestionMessage.setResult("评论举报失败,未刷新当前页面");
        }
        return reportQuestionMessage;
    }

    public PageBean<AnswerComment> listReportedComments() {
        //查询
        List<AnswerComment> comments = this.selectAll().stream()
                .filter(answerComment -> answerComment.getReportStatus() == 0)
                .collect(Collectors.toList());

        //返回数据
        return new PageBean<>(comments.size(), comments);
    }

    public Message<AnswerComment> agreedReportComment(int agreedReportCommentId) throws Exception {
        //查找数据
        AnswerComment comment = this.selectAll().stream()
                .filter(comment1 -> comment1.getId().equals(agreedReportCommentId))
                .collect(Collectors.toList()).get(0);
        //初始化
        Message<AnswerComment> commentMessage = new Message<>(null, "审核被举报的评论失败，请重新尝试", 0);

        //校验
        if (comment == null) {
            commentMessage.setResult("当前审核的举报评论为空");
            return commentMessage;
        }

        //修改 :-1表示管理员通过举报审核，1表示不通过举报审核，0表示仍未审核
        comment.setReportStatus(-1);

        System.out.println("审核的举报评论为 " + comment);

        //调用dao
        int update = this.update(comment);

        if (update > 0) {
            System.out.println("agreedReportCommentSuccess");
            commentMessage.setResult("审核成功,已刷新当前页面");
            commentMessage.setStatus(1);
            commentMessage.setT(comment);
        } else {
            commentMessage.setResult("审核失败,未刷新当前页面");
        }
        return commentMessage;
    }

    public Message<AnswerComment> disagreedReportComment(int disagreedReportCommentId) throws Exception {
        //查找数据
        AnswerComment comment = this.selectAll().stream()
                .filter(comment1 -> comment1.getId().equals(disagreedReportCommentId)).collect(Collectors.toList()).get(0);
        //初始化
        Message<AnswerComment> questionMessage = new Message<>(null, "审核被举报的评论失败，请重新尝试", 0);

        //校验
        if (comment == null) {
            questionMessage.setResult("当前审核的举报评论为空");
            return questionMessage;
        }

        //修改 :-1表示管理员通过举报审核，1表示不通过举报审核，即该问题是正常的，0表示仍未审核
        comment.setReportStatus(1);
        comment.setReportIllegalReason("无");

        System.out.println("管理员反驳审核的评论为 " + comment);

        //调用dao
        int update = this.update(comment);

        if (update > 0) {
            System.out.println("disagreedReportCommentSuccess");
            questionMessage.setResult("审核成功,已刷新当前页面");
            questionMessage.setStatus(1);
            questionMessage.setT(comment);
        } else {
            questionMessage.setResult("审核失败,未刷新当前页面");
        }
        return questionMessage;
    }


}
